﻿Imports System.Web.UI
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Configuration

Public Class CLS_Rpt

    Protected _CRConnectionInfo As ConnectionInfo
    Protected _Nombre_Reporte As String

    Public Property Nombre_Reporte() As String
        Get
            Return _Nombre_Reporte
        End Get
        Set(ByVal value As String)
            _Nombre_Reporte = value
        End Set
    End Property

    Sub New()
        _crConnectionInfo = New ConnectionInfo()
        _CRConnectionInfo.ServerName = ConfigurationManager.AppSettings("cnn_server")
        _CRConnectionInfo.DatabaseName = ConfigurationManager.AppSettings("Rpt_Database_Name")
        _CRConnectionInfo.UserID = ConfigurationManager.AppSettings("Rpt_User_ID")
        _CRConnectionInfo.Password = ConfigurationManager.AppSettings("Rpt_Password")

        _Nombre_Reporte = "SinTitulo.pdf"
    End Sub

    Sub New(ByVal Archivo As String)
        _CRConnectionInfo = New ConnectionInfo()
        _CRConnectionInfo.ServerName = ConfigurationManager.AppSettings("Rpt_Server_Name")
        _CRConnectionInfo.DatabaseName = ConfigurationManager.AppSettings("Rpt_Database_Name")
        _CRConnectionInfo.UserID = ConfigurationManager.AppSettings("Rpt_User_ID")
        _CRConnectionInfo.Password = ConfigurationManager.AppSettings("Rpt_Password")

        _Nombre_Reporte = Archivo
    End Sub


    Sub ViewReport(ByVal OC As Integer)
        Dim reporte As New Rpt_OrdenCompra



        reporte.SetParameterValue("@i_cPeriodo", OC)

        Dim tables As Tables
        tables = reporte.Database.Tables
        For i = 0 To tables.Count - 1
            Dim tableLogonInfo As TableLogOnInfo = tables(i).LogOnInfo
            tableLogonInfo.ConnectionInfo = _CRConnectionInfo
            tables(i).ApplyLogOnInfo(tableLogonInfo)
        Next

        export(reporte)
    End Sub

    Sub ExportReport(ByVal OC As Integer)
        Dim reporte As New Rpt_OrdenCompra



        reporte.SetParameterValue("@i_cPeriodo", OC)

        Dim tables As Tables
        tables = reporte.Database.Tables
        For i = 0 To tables.Count - 1
            Dim tableLogonInfo As TableLogOnInfo = tables(i).LogOnInfo
            tableLogonInfo.ConnectionInfo = _CRConnectionInfo
            tables(i).ApplyLogOnInfo(tableLogonInfo)
        Next

        export(reporte)
    End Sub

    Private Sub ViewerPDF(ByVal rpt As Object, ByVal Nombre As String)
        rpt.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, True, Nombre)
    End Sub

    Private Sub export(ByVal rpt As Object)
        ViewerPDF(rpt, _Nombre_Reporte)
    End Sub
   
End Class